class Product:

    def __init__(self, name):
        self.name = name

    def get_name(self):
        return self.name


class ProductFactory:
    _product_cache = {}

    @staticmethod
    def create_product(name):

        if name in ProductFactory._product_cache:
            return ProductFactory._product_cache[name]
        else:
            product = Product(name)
            ProductFactory._product_cache[name] = product
            return product


# 使用对象工厂创建对象并进行缓存
product_factory = ProductFactory()
product_a = product_factory.create_product("A")
product_b = product_factory.create_product("B")
product_a_cached = product_factory.create_product("A")  # 从缓存中获取对象

print(product_a)
print(product_b)
print(product_a_cached)

print(product_a.get_name())
print(product_b.get_name())
print(product_a_cached.get_name())

'''
<__main__.Product object at 0x000002DA28047F70>
<__main__.Product object at 0x000002DA28047DF0>
<__main__.Product object at 0x000002DA28047F70>
A
B
A
'''